package com.yunpuvip.sachet.modular.util.sms;


import org.slf4j.Logger;
import org.slf4j.LoggerFactory;


/**
 *
 * 功能描述：互亿无线接口 ： http://www.ihuyi.com
 * <p>
 * 版权所有：广东移领科技有限公司
 * <p>
 * 未经本公司许可，不得以任何方式复制或使用本程序任何部分
 *
 * @author tiger_lin 新增日期：2016年3月10日
 * @author tiger_lin 修改日期：2016年3月10日
 *
 */
public class Sendsms {
    private final static Logger LOGGER = LoggerFactory.getLogger(Sendsms.class);

    public static String getSmsCode() {
        String mobile_code = (int) ((Math.random() * 9 + 1) * 100000) + "";
        return mobile_code;
    }

    /**
     * 发送短信验证码公共方法
     *
     * @Param: [telNum]
     * @return: java.lang.String
     * @Author: hyf
     * @Date: 2018/7/18
     */
    public static String sendSmsCodeCommon(String telNum, Integer sendType) {
        String code = null;
        try {
            code = SendSmsUtil.sendSmsCode(telNum, sendType);
            //this.cacheSmscode(telNum, code);
            LOGGER.info("手机号:{};验证码:{}", telNum, code);
            //System.out.println(StrUtil.format("手机号:{};验证码:{}", telNum, code));
            return code;
        } catch (Exception e) {
            e.printStackTrace();
            /*TeweiRestExceptionEnum exception = TeweiRestExceptionEnum.SMS_CODE_SEND_ERROR;
            exception.setExceptionStr("手机号：" + telNum);
            throw new TeweiRestException(exception);*/
            return null;
        }
    }


    /*/**
     * 缓存短信验证码
     * @Param: [phoneNum, code]
     * @return: boolean
     * @Author: hyf
     * @Date: 2018/7/16
     *//*
    private boolean cacheSmscode(String phoneNum,String code){
        PhoneVerificationCode phoneVerificationCode=new PhoneVerificationCode(phoneNum,code,LocalDateTime.now());
        CacheKit.put(CacheConstant.EcacheName.USER_CACAHE,CacheConstant.EcachePrefix.PHONE_VERIFICATION_CODE+phoneNum, phoneVerificationCode);
        return true;
    }
*/

   /* *//* 校验手机验证码
     * @Param: [phoneNum, code]
     * @return: void
     * @Author: hyf
     * @Date: 2018/7/17
     *//*
    private void checkSmsCode(String phoneNum,String code){
        //check( code != null && code.equal( getCode(phone), "check.faile.exception.code");
        PhoneVerificationCode phoneVerificationCode=CacheKit.get(ApplicationConstant.EcacheName.VERIFY_CODE,CacheConstant.EcachePrefix.PHONE_VERIFICATION_CODE+phoneNum);
        if (phoneVerificationCode!=null){
            Duration duration = Duration.between(phoneVerificationCode.getDate(), LocalDateTime.now());
            //验证码已失效
            if (duration.toMinutes()> ApplicationConstant.SMS_CODE_CACHE_TIME){
                throw new TeweiRestException(TeweiRestExceptionEnum.SMS_CODE_INVALID);
            }
            //验证码错误
            if (code.equals(phoneVerificationCode.getCode())){
                return;
            }else{
                throw new TeweiRestException(TeweiRestExceptionEnum.SMS_CODE_CHECK_ERROR);
            }
        }
        throw new TeweiRestException(TeweiRestExceptionEnum.SMS_CODE_NOT_EXIST);
    }*/

   /* *//**
     * 修改手机号码
     *
     * @Param: [phoneVerificationCode, newPhone, userWrapper]
     * @return: com.yunpuvip.tewei.core.base.response.ServerResponse
     * @Author: hyf
     * @Date: 2018/7/19
     *//*
    @RequestMapping("change_phone")
    public ServerResponse changePhone(String phoneVerificationCode, String newPhone, @RequestAttribute("userWrapper") UserWrapper userWrapper) {
        super.checkUserIsTwuser(userWrapper);
        if (Validator.isEmpty(phoneVerificationCode)) {
            throw new TeweiRestException(TeweiRestExceptionEnum.FIELD_CHECK_FAILED);
        }
        if (!Validator.isMobile(newPhone)) {
            throw new TeweiRestException(TeweiRestExceptionEnum.PHONE_FORMAT_ERROR);
        }

        iTwuserService.updatePhone(newPhone, phoneVerificationCode, userWrapper.getUserId());
        return ServerResponse.creatBySuccess();
    }*/


}
